package com.kaifamiao.dao.impl;

import com.kaifamiao.dao.IAccountDao;
import com.kaifamiao.model.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.*;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author lucien-ma
 * @date 2021/8/27
 * @apiNote
 */
@Repository
public class IAccountDaoImpl implements IAccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Account> findAll() {
        System.out.println("IAccountDaoImpl::findAll...正在执行");
        return jdbcTemplate.query("select * from account",
                new BeanPropertyRowMapper<>(Account.class));
    }

    @Override
    public Account findById(Integer id) {
        System.out.println("根据 id 查询了一条记录::findById");
        return jdbcTemplate.queryForObject("select * from account where id = ?",
                new BeanPropertyRowMapper<>(Account.class), id);
    }

    @Override
    public int updateMoney(Account account) {
        return jdbcTemplate.update("update account set money = money + (?) where id = ?",
                account.getMoney(), account.getId());
    }
}
